package com.shijichaoxing.dao.impl;

import com.shijichaoxing.dao.AdminDao;
import com.shijichaoxing.entity.Admin;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zj
 */
@Repository
public class AdminDaoImpl implements AdminDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Admin> login(Admin admin) {
        String sql = "select * from admin where real_name= ? and password= ?";
        System.out.println(admin.getRealName());
        return jdbcTemplate.query(sql, ps -> {
            ps.setString(1, admin.getRealName());
            ps.setString(2, admin.getPassword());
        }, new AdminRowMapper());
    }

    @Override
    public boolean addAdmin(Admin admin) {
        String sql = "INSERT INTO admin(" +
                "real_name, " +
                "password" +
                ") VALUES(?, ?)";

        int result = jdbcTemplate.update(sql, ps -> {
            ps.setString(1, admin.getRealName());
            ps.setString(2, admin.getPassword());
        });
        return result > 0;
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        String sql = "UPDATE admin SET " +
                "real_name = ? ," +
                "password = ?" +
                "WHERE id = ?";
        int result = jdbcTemplate.update(sql, ps -> {
            ps.setString(1, admin.getRealName());
            ps.setString(2, admin.getPassword());
            ps.setInt(3, admin.getId());
        });
        return result > 0;
    }

    @Override
    public Admin findAdminById(Integer id) {
        String sql = "select * from admin where id = ?";
        return jdbcTemplate.queryForObject(sql, new AdminRowMapper(), id);
    }

    @Override
    public List<Admin> findAdminByPage(int start, int limit, String indexName, boolean isAsc) {
        String upDown = isAsc ? "ASC" : "DESC";
        String sql = "select * from admin ORDER BY %s %s limit ?,?";
        return jdbcTemplate.query(String.format(sql, indexName, upDown), ps -> {
            ps.setInt(1, start);
            ps.setInt(2, limit);
        }, new AdminRowMapper());
    }

    @Override
    public int countAll() {
        String sql = "SELECT COUNT(*) FROM admin";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql = "DELETE FROM admin WHERE id = ?";
        int result = jdbcTemplate.update(sql, ps -> ps.setLong(1, id));
        return result > 0;
    }

    class AdminRowMapper implements RowMapper<Admin> {
        @Override
        public Admin mapRow(ResultSet rs, int i) throws SQLException {
            Admin admin = new Admin();
            admin.setId(rs.getInt("id"));
            admin.setRealName(rs.getString("real_name"));
            admin.setPassword(rs.getString("password"));
            admin.setCreateTime(rs.getTimestamp("create_time"));
            admin.setUpdateTime(rs.getTimestamp("update_time"));
            return admin;
        }
    }
}
